<?php
namespace app\mpbase\controller;

use app\mpbase\service\Config as ConfigService;
use app\mpbase\service\Wechat as WechatService;
use app\common\model\Order as OrderModel;
use app\common\model\User as UserModel;
use app\common\model\WechatAutoreply;
use app\common\model\WechatConfig;
use app\common\model\WechatContext;
use app\common\model\WechatResponse;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use think\Controller;
use think\Log;
use think\Request;

/**
 * 微信控制器
 */
class Wechat extends Controller
{
    //消息推送
    public function api_redirect()
    {
        $wechatApp = new \app\mpbase\library\WechatApp();
        $wechatApp->api();
    }

    //Xdebug断点调试 拼接ideKey
    public function api()
    {
        $url = str_replace('api','api_redirect',request()->url(true))."&XDEBUG_SESSION_START=PHPSTORM";
        Log::log('Wechat_Request_Url:'.$url);
        $xmlData = file_get_contents('php://input');
        $result = curl_post_raw($url,$xmlData);
        return $result;
    }
}

function curl_post_raw($url, $rawData)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'Content-Type: application/json'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}